﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MethodWorx.Core.Extensions
{
    public static class IServiceProviderExtensions
    {
        public static T GetService<T>(this IServiceProvider provider)
        {
            return (T)provider.GetService(typeof(T));
        }

        public static void TryInvoke<T>(this IServiceProvider provider, Action<T> t)
        {
            T service = provider.GetService<T>();
            if (service != null)
                t(service);
        }

        public static TResult TryInvoke<T, TResult>(this IServiceProvider provider, Func<T, TResult> t)
        {
            T service = provider.GetService<T>();
            if (service != null)
                return t(service);

            return default(TResult);
        }
    }
}
